package com.example.demo.controller;

import com.example.comon.domain.LoginUser;
import com.example.comon.domain.R;
import com.example.comon.utils.UserUtil;
import com.example.demo.domain.Bo.AddMenuBo;
import com.example.demo.domain.Bo.MenuListBo;
import com.example.demo.domain.Menu;
import com.example.demo.domain.Vo.MenuListVo;
import com.example.demo.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
@Api(value = "menu",tags = "菜单控制器")
public class MenuController {

    @Autowired
    private IMenuService menuService;


    @GetMapping("/getMenuList")
    @ApiOperation("获取菜单列表")
    public R<List<MenuListVo>> getMenuList(MenuListBo bo){
        Menu menu = new Menu();
        menu.setMenuName(bo.getMenuName());
        List<MenuListVo>list = menuService.selectMenu(menu);
        return R.success(list);
    }

    @PostMapping("/addMenu")
    @ApiOperation("增加菜单")
    public R<Menu> addMenu(@RequestBody AddMenuBo bo){
        Menu menu = new Menu();
        menu.setMenuName(bo.getMenuName());
        menu.setRouter(bo.getRouter());
        menu.setLevel(bo.getLevel());
        menu.getFatherMenuId(bo.getFatherMenuId());
        menu.setCreateBy(bo.getCreateBy());
        menuService.insertMenu(menu);
        return R.success(menu);
    }

    @PostMapping("/updateMenuById")
    @ApiOperation("修改菜单")
    public R updateMenuById(@RequestBody Menu menu){
        menuService.updateMenuById(menu);
        return R.success();
    }

    @DeleteMapping("/delMenuById")
    @ApiOperation("删除菜单")
    public R delMenuById(long menuId){
        menuService.delMenuById(menuId);
        return R.success();
    }

    @GetMapping("getUserById")
    @ApiOperation("获取用户菜单列表")
    public R<List<Menu>> getByUserId(){
        Integer userId = UserUtil.getLoginUser().getUserId();
        List<Menu> menus = menuService.getByUserId(Long.valueOf(userId));
        return R.success(menus);
    }

}
